/*
 * meituan.com Inc.
 * Copyright (c) 2010-2018 All Rights Reserved.
 */
package java.curator;

import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.framework.recipes.leader.LeaderLatch;
import org.apache.curator.framework.recipes.leader.LeaderLatchListener;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.zookeeper.CreateMode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.Random;

/**
 * <p>
 *
 * </p>
 * @author LvJing
 * @version $Id:Master.java v1.0 2018/9/9 上午9:52 LvJing Exp $
 */
public class Master {

    private static final Logger log = LoggerFactory.getLogger(Master.class);

    private String serverId = Integer.toHexString(new Random().nextInt());

    private CuratorFramework client;

    public Master() {
        client = CuratorFrameworkFactory.newClient("127.0.0.1:2181",
                new ExponentialBackoffRetry(1000, 3));
        client.start();
        client.getCuratorListenable().addListener((curatorFramework, curatorEvent) -> {
            switch (curatorEvent.getType()) {
                case CREATE:
                    log.info("create:{}", curatorEvent.getName());
                case CHILDREN:
                    log.info("getChildren{}", curatorEvent.getChildren());
            }
        });
    }

    public void close() {
        client.close();
    }

    public void runForMaster() {
        try {
            String createResult = client.create().withMode(CreateMode.PERSISTENT).inBackground()
                    .withUnhandledErrorListener((s, throwable) -> {
                        log.error("create error:{}", s, throwable);
                    }).forPath("/master", serverId.getBytes());
            log.info("create return:{}", createResult);
        } catch (Exception e) {
            log.error("create error", e);
        }
    }

    public void getChildren() {
        try {
            List<String> getChildrenResult = client.getChildren().inBackground((curatorFramework, curatorEvent) -> {
                switch (curatorEvent.getType()) {
                    case CREATE:
                        log.info("create...");
                    case CHILDREN:
                        log.info("getChildren...");
                }
            }).withUnhandledErrorListener((s, throwable) -> {

            }).forPath("/zookeeper");
            log.info("getChildren return:{}", getChildrenResult);
        } catch (Exception e) {
            log.error("getChildren error", e);
        }
    }

    public void isLeader() {
        LeaderLatch latch = new LeaderLatch(client, "/master");
        LeaderLatchListener listener = new LeaderLatchListener() {
            @Override
            public void isLeader() {

            }

            @Override
            public void notLeader() {

            }
        };
    }

    public static void main(String[] args) throws InterruptedException {
        Master master = new Master();
        //master.runForMaster();
        master.getChildren();
        Thread.sleep(5000);
        master.close();
    }
}
